package com.xwdeshj.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xwdeshj.entity.XwSceincMerchanttype;
import com.xwdeshj.entity.XwSceincPlaytype;
import com.xwdeshj.repository.XwSceincPlaytypeRepository;
import com.xwdeshj.service.XwSceincPlaytypeService;
import com.xwdeshj.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by BKWJA on 2018/12/21.
 * 景区游玩类型实现层
 */
@Slf4j
@Service("xwSceincPlaytypeService")
public class XwSceincPlaytypeServiceImpl implements XwSceincPlaytypeService {

    @Autowired
    private XwSceincPlaytypeRepository xwSceincPlaytypeRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private Sid sid;

    @Override
    public JSONObject getAllScePlaytype(HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String sql = "select sceinc_palyType_id,sceinc_playType_encryptId,sceinc_playType_desc from xw_sceinc_playtype order by sceinc_playType_createtime desc ";
        try {
            SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
            JSONArray jsonArray = new JSONArray();
            while (rs.next()){
                JSONObject object = new JSONObject();
                object.put("sceplayId",rs.getString("sceinc_palyType_id"));
                object.put("encryptId",rs.getString("sceinc_playType_encryptId"));
                object.put("title",(rs.getString("sceinc_playType_desc")==null?"":rs.getString("sceinc_playType_desc")));
                jsonArray.add(object);
            }
            result.put("list",jsonArray);
            result.put("status",0);
            result.put("msg","ok");
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg",e);
        }
        System.out.println("getAllScePlaytype："+result.toString());
        return  result;
    }

    @Override
    public JSONObject getScePlaytypelist(Integer pageindex, Integer pagesize, String starttime,
                                         String endtime, String sortfild, String sortway) {
        JSONObject result = new JSONObject();
        String sql = "select sceinc_palyType_id,sceinc_playType_encryptId,sceinc_playType_desc,sceinc_playType_updatetime,sceinc_playType_createtime from xw_sceinc_playtype ";
        StringBuffer sb = new StringBuffer();
        sb.append(" 1=1 ");
        if(!starttime.equals("")){
            starttime += " 00.00.01";
            endtime += " 23.59.59";
            sb.append(" and sceinc_playType_createtime >= '"+starttime+"' and sceinc_playType_createtime <= '"+ endtime + "' ");
        }
        //排序
        if (!sortfild.equals("")){
            sb.append(" order by "+sortfild+" " +sortway + " ");
        }
        try {
            sql = sql + " where " +sb.toString() + " limit "+ (pageindex-1)*pagesize + "," + pagesize;
            //分页SQL
            String sql2 = "select count(*) from xw_sceinc_playtype where " + sb.toString();
            SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
            SqlRowSet rs2 = jdbcTemplate.queryForRowSet(sql2);
            result.put("status",0);
            result.put("msg","ok");
            while (rs2.next()){
                Integer count = rs2.getInt(1);
                result.put("recordcount",count);
                if (count%pagesize == 0){
                    result.put("pagecount",count/pagesize);
                }else {
                    result.put("pagecount",count/pagesize + 1);
                }
            }
            JSONArray jsonArray = new JSONArray();
            while (rs.next()){
                JSONObject object = new JSONObject();
                object.put("sceinc_palyType_id",rs.getString("sceinc_palyType_id"));
                object.put("sceinc_playType_encryptId",rs.getString("sceinc_playType_encryptId"));
                object.put("sceinc_playType_desc",(rs.getString("sceinc_playType_desc")==null?"":rs.getString("sceinc_playType_desc")));
                object.put("sceinc_playType_updatetime", DateUtil.DateToString(rs.getDate("sceinc_playType_updatetime")));
                object.put("sceinc_playType_createtime",DateUtil.DateToString(rs.getDate("sceinc_playType_createtime")));
                jsonArray.add(object);
            }
            result.put("list",jsonArray);
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg",e);
        }
        System.out.println("getScePlaytypelist:"+result.toString());
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject updateScePlaytype(String sceinc_playType_encryptId, String sceinc_playType_desc) {
        JSONObject result = new JSONObject();
        if(sceinc_playType_encryptId.equals("")){
            XwSceincPlaytype sceincPlaytype = new XwSceincPlaytype();
            sceincPlaytype.setSceincPlaytypeEncryptid(sid.nextShort());
            sceincPlaytype.setSceincPlaytypeDesc(sceinc_playType_desc);
            sceincPlaytype.setSceincPlaytypeCreatetime(new Date());
            sceincPlaytype.setSceincPlaytypeUpdatetime(new Date());

            try {
                xwSceincPlaytypeRepository.save(sceincPlaytype);
                result.put("status",0);
                result.put("msg","ok");
            }catch (Exception e){
                e.printStackTrace();
                result.put("status",500);
                result.put("msg",e);
            }
        }else {
            XwSceincPlaytype xwSceincPlaytype = xwSceincPlaytypeRepository.findBySceincPlaytypeEncryptid(sceinc_playType_encryptId);

            if (xwSceincPlaytype != null){
                xwSceincPlaytype.setSceincPlaytypeDesc(sceinc_playType_desc);
                xwSceincPlaytype.setSceincPlaytypeUpdatetime(new Date());
                try {
                    xwSceincPlaytypeRepository.save(xwSceincPlaytype);
                    result.put("status",0);
                    result.put("msg","ok");
                }catch (Exception e){
                    e.printStackTrace();
                    result.put("status",500);
                    result.put("msg",e);
                }
            }else {
                result.put("status",500);
                result.put("msg","该记录不存在！");
            }

        }
        System.out.println("updateScePlaytype:"+result.toString());
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject delScePlaytype(String sceinc_playType_encryptId) {
        JSONObject result = new JSONObject();
        if(!"".equals(sceinc_playType_encryptId) && sceinc_playType_encryptId != null){
            try {
                if(sceinc_playType_encryptId.indexOf(",") == -1){
                    //单个删除
                    int flag =  xwSceincPlaytypeRepository.deleteBySceincPlaytypeEncryptid(sceinc_playType_encryptId);
                    if(flag == 1){
                        result.put("status",0);
                        result.put("msg","ok");
                    }else {
                        result.put("status",500);
                        result.put("msg","删除失败！");
                    }
                }else {
                    //批量删除
                    int flag = 0;
                    String[] tids = sceinc_playType_encryptId.split(",");
                    for (int i = 0; i < tids.length; i++) {
                        flag = xwSceincPlaytypeRepository.deleteBySceincPlaytypeEncryptid(tids[i]);
                    }
                    if(flag == 1){
                        result.put("status",0);
                        result.put("msg","ok");
                    }else {
                        result.put("status",500);
                        result.put("msg","删除失败！");
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                result.put("status",500);
                result.put("msg",e);
            }

        }else {
            result.put("status",500);
            result.put("msg","sceinc_playType_encryptId不能为空！");
        }
        System.out.println("delScePlaytype："+result);
        return result;
    }
}
